package com.yntsoft.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springside.modules.cache.memcached.SpyMemcachedClient;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yntsoft.entity.Role;
import com.yntsoft.entity.UserRole;
import com.yntsoft.mapper.RoleMapper;
import com.yntsoft.mapper.UserRoleMapper;
import com.yntsoft.mybatisUtil.Page;
import com.yntsoft.util.DateUtil;

@Service
public class RoleService {


	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private UserRoleMapper userRoleMapper;
	
	//注入 memcached的客服端
	@Autowired(required = false)
	protected SpyMemcachedClient memcachedClient;

	/**
	 * 查询角色列表
	 * @param map
	 * @return
	 */
	public PageInfo<Role> queryRoleList(Map<String, Object> searchParam,Integer pageSize) {
		Integer currentpage = 1;
		if(null != searchParam.get("currentpage") && !searchParam.get("currentpage").equals("")) {
			currentpage = Integer.parseInt(searchParam.get("currentpage").toString());
		}
		PageHelper.startPage(currentpage,pageSize);
		List<Role> roleList = roleMapper.queryRoleList(searchParam);
		for (int i = 0; i < roleList.size() ; i++) {
			Role role = roleList.get(i);
			role.setTimeStr(DateUtil.dateToStr(role.getCreateTime()));
		}
		PageInfo<Role> pageInfo = new PageInfo<Role>(roleList);
		return pageInfo;
	}

	/**
	 * 保存角色信息
	 * @param role
	 * @return
	 */
	public Integer saveRole(Role role) {
		Integer rst = 0 ;
		if( role.getId() == null){ 
			role.setCreateTime(new Date());
			role.setIsLock(Role.IS_LOCK_2);//正常
			rst = roleMapper.insertSelective(role);
		}else{
			 rst = roleMapper.updateByPrimaryKeySelective(role);
		}
		return rst;
	}

	/**
	 * 获取角色信息 
	 * @param roleId
	 * @return
	 */
	public Role getRoleInfo(Integer roleId) {
		return roleMapper.selectByPrimaryKey(roleId);
	}

	public Integer roleDel(String ids) {
		String[] realId = ids.split(",");
		List<String> idList = new ArrayList<>();
		for(int i = 0 ;i < realId.length ;i++ ){
			idList.add(realId[i]);
		}
		return roleMapper.roleDel(idList);
	}

	/**
	 * 获取用户角色
	 * @param userId
	 * @return
	 */
	public List<Role> getUserRole(Integer userId) {
		List<Role> roleList = roleMapper.queryAllRole();
		List<Role> userRole = roleMapper.queryByUserId(userId);
		for(int i = 0 ; i < roleList.size() ; i++ ){
			Role role = roleList.get(i);
			for(int j = 0 ; j < userRole.size() ; j++ ){
				Role uRole = userRole.get(j);
				if(role.getId() == uRole.getId()){
					role.setUserIsRole(1);//设置为用户拥有着个角色
					break;
				}
			}
		}
		return roleList;
	}

	/**
	 * 设置用户角色
	 * @param roleIds
	 * @return
	 */
	public Integer setUserRole(String roleIds,Integer userId) {
		String[] realRoleId = roleIds.split(",");
		//删除用户已经拥有的角色
		userRoleMapper.deleteByUserId(userId);
		Integer rst = 0;
		//为用户添加新角色
		for(int i = 0 ; i < realRoleId.length ; i++) {
			UserRole role = new UserRole();
			role.setUserId(userId);
			role.setRoleId(Integer.valueOf(realRoleId[i]));
			Integer rowRst = userRoleMapper.insertSelective(role);
			if(rowRst != null && rowRst != 0){
				rst = 1;
			}else{
				try {
					throw new Exception();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return rst;
	}

    public Page<Role> queryPageRoleList(Map<String, Object> searchParam, Integer pagesize) {
        
        Integer currentpage = 1;
        if(null != searchParam.get("currentpage") && !searchParam.get("currentpage").equals("")) {
            currentpage = Integer.parseInt(searchParam.get("currentpage").toString());
        }
        Page<Role> page = new Page<Role>();
        List<Role> roleList = roleMapper.queryPageRoleList(page);
        for (int i = 0; i < roleList.size() ; i++) {
            Role role = roleList.get(i);
            role.setTimeStr(DateUtil.dateToStr(role.getCreateTime()));
        }
        
        page.setDatas(roleList);
        return page;
    }  
}
